package com.business.api.official.toursism.websitegoods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.business.api.official.toursism.websitegoods.dto.WebsiteGoodsApiDTO;
import com.business.api.official.toursism.websitegoods.from.WebsiteGoodsApiSaveFrom;
import com.business.api.official.toursism.websitegoods.from.WebsiteGoodsApiUpdateFrom;
import com.business.api.official.toursism.websitegoods.manager.WebsiteGoodsApiManager;
import com.business.api.official.toursism.websitegoods.query.WebsiteGoodsApiPageQuery;
import com.business.api.official.toursism.websitegoods.query.WebsiteGoodsApiQuery;
import com.business.api.official.toursism.websitegoods.service.WebsiteGoodsApiService;
import com.business.api.official.toursism.websitegoods.vo.WebsiteGoodsApiBriefPageVO;
import com.business.api.official.toursism.websitegoods.vo.WebsiteGoodsApiDetailListVO;
import com.business.api.official.toursism.websitegoods.vo.WebsiteGoodsApiDetailPageVO;
import com.business.api.official.toursism.websitegoods.vo.data.WebsiteGoodsApiBriefData;
import com.business.api.official.toursism.websitegoods.vo.data.WebsiteGoodsApiDetailData;
import com.business.system.official.toursism.websitegoods.entity.WebsiteGoods;
import com.business.system.official.toursism.websitegoods.service.WebsiteGoodsService;
import com.seed.core.exception.ManagerException;
import com.seed.core.exception.ServiceException;
import com.seed.core.pojo.dto.SeedMessageDTO;
import com.seed.core.pojo.service.impl.SeedApiServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @description 商品
 * @author CodeGenerator
 * @date   2025-01-25
 * @version V1.0
 */
@Slf4j
@Service("websiteGoodsApiService")
@Transactional(rollbackFor = ServiceException.class)
public class WebsiteGoodsApiServiceImpl extends SeedApiServiceImpl<WebsiteGoodsApiManager, WebsiteGoods> implements WebsiteGoodsApiService {

    @Autowired
    private WebsiteGoodsService systemService;


    /**
     * 持久化
     *
     * @param websiteGoodsFrom 需要保存的对象
     * @return id，如果保存失败返回空字符串
     * @throws ServiceException 业务层异常
     */
    @Override
    public WebsiteGoodsApiDTO save(WebsiteGoodsApiSaveFrom websiteGoodsFrom) throws ServiceException {
        WebsiteGoodsApiDTO apiDTO = new WebsiteGoodsApiDTO();
        WebsiteGoods websiteGoods = new WebsiteGoods();
        try {
            BeanUtils.copyProperties(websiteGoodsFrom, websiteGoods, "id");
        } catch (Exception e) {
            throw new ServiceException(e, websiteGoodsFrom);
        }
        SeedMessageDTO baseSeedDTO = this.save(websiteGoods);
        if (!baseSeedDTO.getSuccess()) {
            apiDTO.setCode(baseSeedDTO.getCode());
            apiDTO.setMessage(baseSeedDTO.getMessage());
            apiDTO.setSuccess(false);
            return apiDTO;
        }
        apiDTO.setWebsiteGoods(websiteGoods);
        return apiDTO;
    }

    /**
     * 部分更新
     *
     * @param websiteGoodsFrom 需要更新的对象
     * @return 是否成功
     * @throws ServiceException 业务层异常
     */
    @Override
    public WebsiteGoodsApiDTO updateSelectiveById(WebsiteGoodsApiUpdateFrom websiteGoodsFrom) throws ServiceException {
        WebsiteGoodsApiDTO apiDTO = new WebsiteGoodsApiDTO();
        if (StringUtils.isEmpty(websiteGoodsFrom.getId())) {
            apiDTO.setCode("A0410");
            apiDTO.setMessage("更新主键为空");
            apiDTO.setSuccess(false);
            return apiDTO;
        }
        WebsiteGoods websiteGoods = new WebsiteGoods();
        try {
            BeanUtils.copyProperties(websiteGoodsFrom, websiteGoods);
        } catch (Exception e) {
            throw new ServiceException(e, websiteGoodsFrom);
        }
        SeedMessageDTO baseSeedDTO = this.updateById(websiteGoods);
        if (!baseSeedDTO.getSuccess()) {
            apiDTO.setCode(baseSeedDTO.getCode());
            apiDTO.setMessage(baseSeedDTO.getMessage());
            apiDTO.setSuccess(false);
            return apiDTO;
        }
        return apiDTO;
    }

    /**
     * 获取全部
     *
     * @param query 查询条件
     * @return 详情VOList
     * @throws ServiceException 业务层异常
     */
    @Override
    public WebsiteGoodsApiDetailListVO listAll(WebsiteGoodsApiQuery query) throws ServiceException {
        WebsiteGoods queryEntity = new WebsiteGoods();
        BeanUtils.copyProperties(query, queryEntity);
        LambdaQueryWrapper<WebsiteGoods> lambdaQueryWrapper = new LambdaQueryWrapper<>(queryEntity);
        List<WebsiteGoods> list = this.list(lambdaQueryWrapper);
        List<WebsiteGoodsApiDetailData> detailVOList;
        try {
            detailVOList = baseManager.listConvertApiDetailData(list);
        } catch (ManagerException e) {
            throw new ServiceException(e, list);
        }
        return new WebsiteGoodsApiDetailListVO(detailVOList);
    }

    /**
     * 简略对象条件查询
     *
     * @param query 查询条件
     * @return 查询出来的简略对象
     * @throws ServiceException 业务层异常
     */
    @Override
    public WebsiteGoodsApiBriefPageVO getBriefPageVO(WebsiteGoodsApiPageQuery query) throws ServiceException {
        Page<WebsiteGoods> page = new Page<>(query.getQueryPage(), query.getQuerySize());
        WebsiteGoods queryEntity = new WebsiteGoods();
        BeanUtils.copyProperties(query, queryEntity);
        LambdaQueryWrapper<WebsiteGoods> lambdaQueryWrapper = new LambdaQueryWrapper<>(queryEntity);
        IPage<WebsiteGoods> pageData = this.page(page, lambdaQueryWrapper);
        List<WebsiteGoods> list = pageData.getRecords();
        List<WebsiteGoodsApiBriefData> voList;
        try {
            voList = baseManager.listConvertApiBriefData(list);
        } catch (ManagerException e) {
            throw new ServiceException(e, list);
        }
        return new WebsiteGoodsApiBriefPageVO(page, voList);
    }

    /**
     * 详细对象条件查询
     *
     * @param query 查询条件
     * @return 查询出来的详细对象
     * @throws ServiceException 业务层异常
     */
    @Override
    public WebsiteGoodsApiDetailPageVO getDetailPageVO(WebsiteGoodsApiPageQuery query) throws ServiceException {
        Page<WebsiteGoods> page = new Page<>(query.getQueryPage(), query.getQuerySize());
        WebsiteGoods queryEntity = new WebsiteGoods();
        BeanUtils.copyProperties(query, queryEntity);
        LambdaQueryWrapper<WebsiteGoods> lambdaQueryWrapper = new LambdaQueryWrapper<>(queryEntity);
        IPage<WebsiteGoods> pageData = this.page(page, lambdaQueryWrapper);
        List<WebsiteGoods> list = pageData.getRecords();
        List<WebsiteGoodsApiDetailData> voList;
        try {
            voList = baseManager.listConvertApiDetailData(list);
        } catch (ManagerException e) {
            throw new ServiceException(e, list);
        }
        return new WebsiteGoodsApiDetailPageVO(page, voList);
    }

}